package com.som.service.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import com.som.bean.data2.input.MergerCompDataInput;
import com.som.bean.data2.output.AllSystemDifferentData;
import com.som.bean.data2.output.AllSystemSameData;
import com.som.bean.data2.output.NotAllData;
import com.som.console.ExcelProcException;
import com.som.console.PathEnum;
import com.som.service.ExcelDataServiceInf;
import com.som.service.excel.ExcelConvDataProcessInf;
import com.som.service.excel.ExcelConvOutputDataProcessInf;
import com.som.service.excel.ExcelWriteProcess;
import com.som.service.excel.ExcelWriteToSheetProcess;
import com.som.service.excel.data2.output.AllSystemDifferentExcelWrite;
import com.som.service.excel.data2.output.AllSystemSameExcelWrite;
import com.som.service.excel.data2.output.NotAllExcelWrite;
import com.som.service.excel.data2.process.AllSystemDifferentOutProcess;
import com.som.service.excel.data2.process.AllSystemSameDataOutProcess;
import com.som.service.excel.data2.process.Data2MergerProcess;
import com.som.service.excel.data2.process.NotAllDataOutProcess;
import com.som.util.FileUtils;

/**
 * 需求二：各系统的数据一致性核对
 * 
 * @author liujun
 * @date 2016年4月8日
 * @verion 0.0.1
 */
public class ExcelData2ServiceImpl implements ExcelDataServiceInf
{
	/**
	 * 将所有输入的数据转换为统一的数据
	 */
	private ExcelConvDataProcessInf<MergerCompDataInput> mergerCompDataConv = new Data2MergerProcess();

	/**
	 * 得到所有相同的数据
	 */
	private ExcelConvOutputDataProcessInf<Map<String, MergerCompDataInput>, AllSystemSameData> sameConv = new AllSystemSameDataOutProcess();

	/**
	 * 写入相同的数据
	 */
	private ExcelWriteProcess<String, AllSystemSameData> writeSameerent = new AllSystemSameExcelWrite();

	/**
	 * 得到所有不同的数据
	 */
	private ExcelConvOutputDataProcessInf<Map<String, MergerCompDataInput>, AllSystemDifferentData> differentConv = new AllSystemDifferentOutProcess();

	/**
	 * 写入不同的数据
	 */
	private ExcelWriteToSheetProcess<String, AllSystemDifferentData> writeDifferent = new AllSystemDifferentExcelWrite();

	/**
	 * 所有系统中都不存在的数据
	 */
	private ExcelConvOutputDataProcessInf<Map<String, MergerCompDataInput>, NotAllData> notallConv = new NotAllDataOutProcess();

	/**
	 * 写入所有系统中都不存在的数据
	 */
	private ExcelWriteToSheetProcess<String, NotAllData> writeNotall = new NotAllExcelWrite();

	public boolean excelDataProcess(Map<String, String> pathMap) throws ExcelProcException
	{
		// 读取所有原始数据信息
		Map<String, MergerCompDataInput> mergerMap = mergerCompDataConv.convProcessData(pathMap);

		// 得到输出列信息
		LinkedList<String> columnList = this.getColumnNum(pathMap);

		// 写入相同的数据
		Map<String, AllSystemSameData> allSameDataMap = sameConv.convOutProcessData(mergerMap, columnList);
		// 写入至文件中
		writeSameerent.writeExcelByMap(pathMap.get(PathEnum.DATA2_OUTPUT_COMPINPUT.getPathFlag()), "same", pathMap, allSameDataMap);

		// 将不同的数据为写入文件的数据
		Map<String, AllSystemDifferentData> allDifferentDataMap = differentConv.convOutProcessData(mergerMap, columnList);
		// 写入至文件中
		writeDifferent.writeExcelByMap(pathMap.get(PathEnum.DATA2_OUTPUT_COMPINPUT.getPathFlag()), "different", pathMap, allDifferentDataMap);

		// 写入所有没有存在过所有系统的数据
		Map<String, NotAllData> notallDataMap = notallConv.convOutProcessData(mergerMap, columnList);
		// 写入至文件中
		writeNotall.writeExcelByMap(pathMap.get(PathEnum.DATA2_OUTPUT_COMPINPUT.getPathFlag()), "notalldata", pathMap, notallDataMap);

		return true;
	}

	/**
	 * 设置列输出信息
	 * 
	 * @param pathMap
	 * @return
	 */
	private LinkedList<String> getColumnNum(Map<String, String> pathMap)
	{
		LinkedList<String> columnList = new LinkedList<String>();

		Iterator<Entry<String, String>> pathIter = pathMap.entrySet().iterator();

		String key = null;
		Entry<String, String> entry = null;

		while (pathIter.hasNext())
		{
			entry = pathIter.next();
			key = entry.getKey();

			// 只有包括标识为多文件的才进行读取
			if (null != key && key.contains(PathEnum.DATA2_INPUT_COMPINPUT.getPathFlag()))
			{
				columnList.add(FileUtils.getFileName(entry.getValue()));
			}
		}

		return columnList;
	}

}
